package com.linkgie.galaxyframework.crypto.context;

import com.linkgie.galaxyframework.crypto.Algorithm;
import com.linkgie.galaxyframework.crypto.AlgorithmCodec;
import com.linkgie.galaxyframework.crypto.CryptoService;
import com.linkgie.galaxyframework.crypto.spi.CryptoFuncSPI;

public abstract class CryptoServiceBase<T extends CryptoFuncSPI> implements CryptoService {

	protected final Algorithm ALGORITHM;

	protected final T INNSER_SERVICE;

	private final Encoding bytesEncoding;
	
	protected final CryptogramEncoding ENCODING;

	public CryptoServiceBase(T innerService) {
		this.INNSER_SERVICE = innerService;
		this.ALGORITHM = innerService.getAlgorithm();
		this.bytesEncoding = Encoding.createInstance(new AlgorithmCodec() {

			@Override
			public Algorithm getAlgorithm(short code) {
				return ALGORITHM.getCode() == code ? ALGORITHM : null;
			}

			@Override
			public Algorithm getAlgorithm(String name) {
				return ALGORITHM.getName().equals(name) ? ALGORITHM : null;
			}
		});
		
		this.ENCODING = new CryptogramEncoding(bytesEncoding);
	}

	@Override
	public final Algorithm getAlgorithm() {
		return ALGORITHM;
	}

	public T getProvidedService() {
		return INNSER_SERVICE;
	}
}
